<?php
namespace ColorIdentify\Controller;
use  Common\Controller\ColorIdentifyController;
class LoginController extends ColorIdentifyController {

    public function add_form_id()
    {
        $third         =   I('post.third','','htmlspecialchars');
        $form_id       =   I('post.form_id','','htmlspecialchars');

        if (! empty($form_id) && ! empty($third))
        {
            $where['third'] =   $third;
            $field          =   array('colour_member_id','third','old_level','level','record');
            $member_data    =   M('colour_member')->where($where)->field($field)->find();

            if (! empty($member_data))
            {
                $form_type      =   1;
                $template_id    =   1;
                $date           =   date('Y-m-d');
                $insert = array(
                    'appid'=>'wx66da7c3d2bc96fc0',
                    'touser'=>$member_data['third'],
                    'template_id'=>$template_id,
                    'page'=>'pages/index/index',
                    'form_id'=>$form_id,
                    'form_type'=>$form_type,
                    'status'=>'-1',
                    'add_time'=>time(),
                    'secret'=>'e1472ac4b86362d3ce31ff5e66899ed9',
                    'createtime'=>$date,
                    'colour_member_id'=>$member_data['colour_member_id']
                );

                $where['touser']     = $member_data['third'];
                $where['createtime'] = $date;
                $where['status']     = '-1';
                $ishave = M('colour_template_send')->field('wxopen_template_send_id')->where($where)->find();
                if ($ishave)
                {
                    M('colour_template_send')->where(array('wxopen_template_send_id'=>$ishave['wxopen_template_send_id']))->save($insert);
                }
                else
                {
                    M('colour_template_send')->data($insert)->add();
                }
            }
        }
    }

    /**
     * 小程序登录
     * @param string
     * @return string
     */
    public function login(){

        $code   =  I('post.code','','htmlspecialchars');
        $appid  =  'wx66da7c3d2bc96fc0';
        $secret =  'e1472ac4b86362d3ce31ff5e66899ed9';
        $share_uid  =   I('post.share_uid','','htmlspecialchars');
        $form_id    =   I('post.form_id','','htmlspecialchars');

        if(empty($code)){
            $data = array();
            $this->returnData($data,$msg='参数错误',$code=1001);
        }

        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

        $res = $this->curlget($url);

        if (!$res['openid']) {
            $this->returnData($res,$msg='openid获取失败',$code=1001);
        }

        $data['third']  =   $res['openid'];

        $where['third'] =   $data['third'];
        $field          =   array('colour_member_id','nick','pic','third');
        $result         =   M('colour_member')->where($where)->field($field)->find();
        if (empty($result))
        {
            $insert_data            =   array();
            $insert_data['third']   =   $data['third'];
            $insert_data['create_time'] =   time();
            M('colour_member')->add($insert_data);
        }

        //绑定好友关系
        if (! empty($share_uid))
        {
            $this->make_friend($result['colour_member_id'] ,$share_uid);
        }
        $this->returnData($data,$msg='openid获取success',$code=200);
    }

    public function update_user_info()
    {
        $third                  =   I('post.third','','htmlspecialchars');
        $user_data              =   array();
        $user_data['pic']       =   I('post.avatarUrl','','htmlspecialchars');
        $user_data['city']      =   I('post.city','','htmlspecialchars');
        $user_data['gender']    =   I('post.gender','','htmlspecialchars');
        $user_data['language']  =   I('post.language','','htmlspecialchars');
        $user_data['nick']      =   I('post.nickName','','htmlspecialchars');
        $user_data['province']  =   I('post.province','','htmlspecialchars');
        $user_data['country']   =   I('post.country','','htmlspecialchars');
        $user_data['iv']        =   I('post.iv','','htmlspecialchars');

        if (! empty($third ) && ! empty($user_data))
        {
            $where          =   array();
            $field          =   array('colour_member_id','third','perfect_time');
            $where['third'] =   $third;
            $member_data    =   M('colour_member')->where($where)->field($field)->find();
            if (! empty($member_data))
            {
                if ($member_data['perfect_time'] == 0 || $member_data['perfect_time'] < time())
                {
                    $where                      =   array();
                    $where['colour_member_id']  =   $member_data['colour_member_id'];
                    $user_data                  =   array_filter($user_data);
                    $user_data['perfect_time']  =   time() + 7*24*3600;
                    $user_data['update_time']   =   time();
                    //$result = M('colour_member')->where($where)->save($user_data);
                    $result     =   M('colour_member')->where($where)->data($user_data)->save(); // 根据条件保存修改的数据
                }
            }
        }
    }

}
